package com.zzxx.wechat.util;

import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;




@Slf4j
public class TraceUtil {

    public final static String TRACE_ID = "traceId";

    public static void appendTraceId() {
        String traceId = getTraceId();
        if (StringUtils.isBlank(traceId)) {
            MDC.put(TRACE_ID, UUID.fastUUID().toString(true));
        }
    }

    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    public static void appendNewTraceId() {
        String traceId = getTraceId();
        if (StringUtils.isBlank(traceId)) {
            MDC.put(TRACE_ID, generateShortRandomUUID());
        } else {
            MDC.put(TRACE_ID, traceId + "_" + generateShortRandomUUID());
        }
    }


    /**
     * 生成短的UUID
     */
    public static String generateShortRandomUUID() {
        return java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 10);
    }

}
